﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TruckScale.Helpers;
using static TruckScale.Helpers.LogHelper;

namespace TruckScale.Models
{
  [Serializable]  //序列化类，用于存储当前用户名密码用于自动登录
  public class UserModel
  {
    public int? UserId { get; set; }
    public string UserName { get; set; }
    public int? UserRole { get; set; }
    public string LoginId { get; set; }
    public string LoginPwd { get; set; }
    public bool IsValid { get; set; } = true;

    public static List<UserModel> SelectAllUser()
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "select * from user where user_id > 1;";
          IEnumerable<UserModel> output = cnn.Query<UserModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }
    public static List<UserModel> SelectValidUser()
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "select * from user where is_valid = 1;";
          IEnumerable<UserModel> output = cnn.Query<UserModel>(sql);
          return output.ToList();
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return null;
        }
      }
    }
    public static UserModel SelectUniqueUserByLoginId(UserModel user)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"select * from user where login_id = '{user.LoginId}' and user_id != '{user.UserId}';";
          UserModel output = cnn.QueryFirstOrDefault<UserModel>(sql);
          return output;
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return new UserModel();
        }
      }
    }
    public static int SelectUsedUser(UserModel user)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"select count(*) from weighrecord where is_valid = 1 and (mzsby = '{user.UserId}' or pzsby = '{user.UserId}');";
          int output = cnn.Query<int>(sql).FirstOrDefault();
          return output;
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int InsertUser(UserModel user)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "insert into user (user_name, user_role, login_id) values (@UserName, @UserRole, @LoginId);";
          return cnn.Execute(sql, user);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int UpdateUser(UserModel user)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = "UPDATE User SET user_name = @UserName, user_role = @UserRole, login_id = @LoginId, login_pwd = @LoginPwd, is_valid = @IsValid WHERE user_id = @UserId;";
          return cnn.Execute(sql, user);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
    public static int DeleteUser(UserModel user)
    {
      using (IDbConnection cnn = DBHelper.DbConnection())
      {
        try
        {
          string sql = $"delete from user where user_id = '{user.UserId}'";
          return cnn.Execute(sql);
        }
        catch (Exception e)
        {
          SetLog(e.Message, LogType.OperationLogger, LogLevel.Error);
          return -1;
        }
      }
    }
  }
}
